import pygame, math, olpcgames.svgsprite

class Tank(pygame.sprite.Sprite):
    '''Tank sprite for the human player (or player 1 over mesh).'''
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        try:
            image = pygame.image.load('data/panzeriv_g.gif')
        except pygame.error, message:
            raise SystemExit, message
        self.image = image.convert()
        image.set_colorkey(image.get_at((0,0)))
        self.rect = self.image.get_rect()
        self.width = self.image.get_width()
        self.height = self.image.get_height()
        
class AITank(pygame.sprite.Sprite):
    '''Tank sprite for the AI (or player 2 over mesh). Displays a mirror image of the Tank sprite.'''
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        try:
            image = pygame.image.load('data/star.png').convert()
        except pygame.error, message:
            raise SystemExit, message
        image.set_colorkey(image.get_at((0,0)))
        self.image = image
        self.rect = self.image.get_rect()
        self.rect = self.image.get_rect()
        self.width = self.image.get_width()
        self.height = self.image.get_height()
    
class Crosshairs(olpcgames.svgsprite.SVGSprite):
    '''Crosshairs sprite for P1's tank'''
    def __init__(self, refx, refy):
        '''Constructor: refx and refy are the center of the arc on which it moves'''
        olpcgames.svgsprite.SVGSprite.__init__(self)
        try:
            image = open('data/drawing.svg').read()
        except IOError:
            raise SystemExit, message
        self.setSVG(image)
        self.rect = self.image.get_rect()
        self.width = self.image.get_width()
        self.height = self.image.get_height()
        self._create_function(refx, refy)
        self.pos = 0
        self.rect.move_ip(self.__table[0])
        
    def _create_function(self, x, y):
        '''Creates lookup table for the crosshairs'''
        self.__table = {}
        for i in range(90):
            self.__table[i] = [x+math.cos(math.radians(i))*100, y-math.sin(math.radians(i))*100]
        
    def update(self, dir):
        '''Move the crosshairs along the angle track'''
        if (self.pos == 0 and dir < 0):
            self.pos = 0
        elif (self.pos == 89 and dir > 0):
            self.pos = 89
        else:
            self.pos += dir
            newpos = self.image.get_rect(center=self.__table[self.pos])
            self.rect = newpos

class Cannonball(pygame.sprite.Sprite):
    '''Cannonball sprite for shooting'''
    def __init__(self, player, tan):
        '''Player is either 0 (player 1) or 1 (player 2), tan is that player's tank object'''
        pygame.sprite.Sprite.__init__(self)
        try:
            image = pygame.image.load('data/cannon_cannonball.gif')
        except pygame.error, message:
            raise SystemExit, message
        self.image = image.convert()
        image.set_colorkey(image.get_at((0,0)))
        self.rect = self.image.get_rect()
        coords = []
        if player == 0:
            coords = tan.rect.topright
        elif player == 1:
            coords = tan.rect.topleft
        self.rect.move_ip(coords)
        self.power = 0
        self.angle = 0
        self.t = 0 #frames in the air/100
        self.dead = False
    
    def do_physics(self):
        '''Calculates x and y components of the bullet's velocity'''
        self.vx = max(math.cos(math.radians(self.angle))*self.power/5, 1)
        self.vyi = max(math.sin(math.radians(self.angle))*self.power/5, 1)
    
    def update(self, maxx, maxy, turn):
        '''Moves the bullet'''
        if (self.rect.center[0] > maxx or self.rect.center[0] < 0 or self.rect.center[1] > maxy):
            self.dead = True
        self.t += .01
        coords = self.rect.center
        delta = self.vyi-(9.8*self.t)
        if (turn):
            self.rect.center = (coords[0]-self.vx, coords[1]-delta)
        else:
            self.rect.center = (coords[0]+self.vx, coords[1]-delta)
        